﻿Public Class UsuarioDAO
    Public Shared Function insertarUsuario(ByVal UsuarioNombre As String, ByVal UsuarioPassword As String, ByVal EmpleadoId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Usuario"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@UsuarioNombre", UsuarioNombre).DbType = DbType.String
                    .AddWithValue("@UsuarioPassword", UsuarioPassword).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerUsuarios(ByVal UsuarioNombre As String) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_Usuario_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@UsuarioNombre", UsuarioNombre).DbType = DbType.String
            da.Fill(ds, "Usuarios")

            Return ds.Tables("Usuarios")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerEmpleadoId(ByVal UsuarioNombre As String, ByVal UsuarioPassword As String) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            Dim EmpleadoId As Integer = -1
            With consulta
                .CommandText = "Select EmpleadoId From Usuario Where UsuarioNombre Like '" + UsuarioNombre + "' And UsuarioPassword Like '" + UsuarioPassword + "'"
                .Connection = cn
                EmpleadoId = .ExecuteScalar()
            End With

            Return EmpleadoId
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarUsuario(ByVal UsuarioNombre As String, ByVal UsuarioPassword As String, ByVal EmpleadoId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Usuario"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@UsuarioNombre", UsuarioNombre).DbType = DbType.String
                    .AddWithValue("@UsuarioPassword", UsuarioPassword).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarUsuario(ByVal UsuarioNombre As String) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Usuario"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@UsuarioNombre", UsuarioNombre).DbType = DbType.String
                    .AddWithValue("@UsuarioPassword", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

End Class
